import decimal

import requests

from utils.logger import GetLogger
from utils.test_data_utils import TestDataUtils
from urllib.parse import urlparse, parse_qs, unquote
import json
logger = GetLogger.get_logger()
def mock_pay(orderNo,premium:decimal):
    url = "http://ms-test.nuanwainfo.com/cashier/nuanwaMockPay"
    headers = {
  "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
}
    payload = {
      'orderNo': orderNo,
      'env': "TEST",
      'premium': premium,
      'platformCode': ""
    }

    try:
        response = requests.post(url, data=payload, headers=headers)
        response.raise_for_status()
        logger.info(f"模拟支付成功:{ response.text}")
    except requests.exceptions.RequestException as e:
        logger.info(f"模拟支付失败:{str(e)}")

def get3factor(gender=2,age=29,day=77,relationToPH=1):
    url = "http://ms-test.nuanwainfo.com/cashier/get3factor"
    params = {
        'gender': gender,
        'age': age,
        'day': day,
        'relationToPH': relationToPH
    }
    try:
        response = requests.get(url, params=params)
        logger.info(f"身份信息生成成功:{response.text}")
        return response.json()
    except requests.exceptions.RequestException as e:
        logger.info(f"身份信息生成失败:{str(e)}")
        data = TestDataUtils.generate_insurance_info()
        logger.info(f"使用兜底信息:{data}")
        return data
def extract_payback_url(input_url):
    # 解析输入的URL
    parsed_url = urlparse(input_url)
    # 提取查询参数
    query_params = parse_qs(parsed_url.query)
    # 获取notify_info参数的值，并进行URL解码
    notify_info_encoded = query_params.get('notify_info', [''])[0]
    notify_info_decoded = unquote(notify_info_encoded)
    # 解析JSON字符串
    notify_info = json.loads(notify_info_decoded)
    # 提取pay_back_url
    pay_back_url = notify_info.get('pay_back_url', '')
    return pay_back_url



if __name__ == '__main__':
    # 示例用法
    input_url = "https://gateway-test.nuanwa.net/ab-flow-platform-renewal/insure/callBack/recieve?sign=c14305c0db9d31c16d93238ac2664b29&notify_info=%7B%22orderNo%22%3A%22AB250522006635494%22%2C%22policyOrderDetailId%22%3A%22126679045%22%2C%22pay_back_url%22%3A%22https%3A%2F%2Fm-test-renewal.aibangbaoxian.net%2Fmf%2Fzakzj%2Fsuccess%3FinstanceCode%3DBWTBCGPAGE%26orderNo%3DAB250522006635494%26sign%3Dc14305c0db9d31c16d93238ac2664b29%26adRuleId%3D100851849%26jumpId%3D100848849%26version%3Dshengji%26pageCode%3DAB202505004%26paymentChannel%3Dinsure_success_upgrade%26uid%3Dnova_uid174789659560323%261%3D1%26adStrategyId%3D100528036%26experienceGroupCode%3Ddefault%26adStyleId%3D100848849%26channelCode%3Dundefined%22%7D&merchantCode=1611344711&orderNo=AB250522006635494"
    output_url = extract_payback_url(input_url)
    print(output_url)
